package com.yihuan.controller;

import com.yihuan.entity.InformEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.InformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/22 19:54
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class InformController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    @Autowired
    InformService informService;
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findInformByInformList")
    public Map<String, Object> findInformByInformList(@RequestBody InformEntity model, HttpSession session) {
        List<InformEntity> findInformByInformList = informService.findInformByInformList(model);
        if (findInformByInformList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.INFORM_LIMIT_FALL);
            return map;
        }
        pageNumber = countUserList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("inform_List", findInformByInformList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.INFORM_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countUserList(InformEntity model) {
        count = informService.countInformList();
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 模糊分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findInformByInformConcatList")
    public Map<String, Object> findInformByInformConcatList(@RequestBody InformEntity model, HttpSession session) {
        List<InformEntity> findInformByInformConcatList = informService.findInformByInformConcatList(model);
        if (findInformByInformConcatList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.INFORM_LIMIT_Concat_FALL);
            return map;
        }
        pageNumber = countUserConcatList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("inform_List", findInformByInformConcatList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.INFORM_LIMIT_Concat_SUCCESS);
        return map;
    }
    
    /**
     * 统计模糊分页查询数据
     *
     * @param model
     * @return
     */
    public int countUserConcatList(InformEntity model) {
        count = informService.countInformConcatList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 删除用户信息状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateInformByInformDeleteState")
    public Map<String, Object> updateInformByInformDeleteState(@RequestBody InformEntity model, HttpSession session) {
        int updateInformByInformDeleteState = informService.updateInformByInformDeleteState(model);
        //判断数据是否存在
        if (updateInformByInformDeleteState == 1) {
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("explain ", MessageConstant.INFORM_DELETE_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain ", MessageConstant.INFORM_DELETE_FALL);
        return map;
    }
    
    /**
     * 新增数据
     *
     * @param inform_image
     * @param inform_title_1
     * @param inform_content_1
     * @param session
     * @return
     */
    
    @RequestMapping("/insertInformByInformData")
    public Map<String, Object> insertInformByInformData(@RequestParam MultipartFile inform_image,
                                                        String inform_title_1,
                                                        String inform_content_1,
                                                        HttpSession session) {
        InformEntity model = new InformEntity();
        if (!inform_image.isEmpty()) {
            String fileName = System.currentTimeMillis() + inform_image.getOriginalFilename();
            String savePath = MessageConstant.file;
            File dest = new File(savePath + File.separator + fileName);
            try {
                inform_image.transferTo(dest);
                model.setInform_deleteState(1);
                //设置时间的格式
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //获取时间的方法
                Date d = new Date();
                //将获取的时间转换成设置的时间格式进行存储
                model.setInform_Date(sf.format(d));
                //用户编号
                model.setUserID(1);
                model.setInform_content(inform_content_1);
                model.setInform_volume(0);
                model.setInform_image(fileName);
                model.setInform_title(inform_title_1);
                int insertUserByUserData = informService.insertInformByInformData(model);
                if (insertUserByUserData == 0) {
                    map.put("code", MessageConstant.FALL_CODE);
                    map.put("explain ", MessageConstant.INFORM_INSERT_FALL);
                    return map;
                }
                map.put("code", MessageConstant.SUCCESS_CODE);
                map.put("explain ", MessageConstant.INFORM_INSERT_SUCCESS);
                return map;
            } catch (IOException e) {
                e.printStackTrace();
                map.put("code", MessageConstant.FALL_CODE);
                map.put("explain ", MessageConstant.INFORM_INSERT_FALL);
                return map;
            }
        } else if (inform_image.isEmpty()) {
            map.put("code", MessageConstant.HALF_CODE);
            map.put("explain ", MessageConstant.INFORM_INSERT_FALL);
        }
        return map;
        
        
    }
    
    
    /**
     * 按月份统计日期
     *
     * @param session
     * @return
     */
    @RequestMapping("/findInformByInformCount")
    public Map<String, Object> findInformByInformCount(HttpSession session) {
        List<InformEntity> findInformByInformCount = informService.findInformByInformCount();
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.INFORM_SELECT_SUCCESS);
        map.put("informList", findInformByInformCount);
        return map;
    }
    
    /**
     * 按编号查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findInformByInformID")
    public Map<String, Object> findInformByInformID(@RequestBody InformEntity model, HttpSession session) {
        //浏览量加1
        int updateInformByInformID = informService.updateInformByInformID(model);
        //查询浏览
        List<InformEntity> findInformByInformID = informService.findInformByInformID(model);
        if (findInformByInformID.size() == 0 || updateInformByInformID == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.INFORM_SELECT_ID_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("inform_List", findInformByInformID);
        map.put("explain ", MessageConstant.INFORM_SELECT_ID_SUCCESS);
        return map;
    }
    
    /**
     * 统计通知
     *
     * @return
     */
    @RequestMapping("/findInformByInform")
    public Map<String, Object> sumOrderList(HttpSession session) {
        int sumOrderList = informService.findInformByInform();
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("sumInform", sumOrderList);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
}
